﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
namespace Andrewl.XnaHelpers.Input
{
    public class Input_GamePad
    {
        #region Variable Declarations

        public GamePadState[] previousGamePadStates;

        public GamePadState[] currentGamePadStates;

        #endregion

        #region Accessors

        public GamePadState[] PreviousGamepadStates
        { get { return previousGamePadStates; } }

        public GamePadState[] CurrentGamepadStates
        { get { return currentGamePadStates; } }

        #endregion

        public Input_GamePad()
        {
            previousGamePadStates = new GamePadState[4];
            currentGamePadStates = new GamePadState[4];
        }

        public void Update()
        {
            for (int i = 0; i < 4; i++)
            {
                previousGamePadStates[i] = currentGamePadStates[i];
                currentGamePadStates[i] = GamePad.GetState((PlayerIndex)i);
            }
        }

        #region Player One Functions

        public bool ButtonDown(Buttons button)
        {
            if (currentGamePadStates[0].IsButtonDown(button))
                return true;
            else
                return false;
        }

        public bool IsButtonPressed(Buttons button)
        {
            if (currentGamePadStates[0].IsButtonDown(button) && previousGamePadStates[0].IsButtonUp(button))
                return true;
            else
                return false;
        }

        public bool IsButtonUp(Buttons button)
        {
            if (currentGamePadStates[0].IsButtonUp(button))
                return true;
            else
                return false;
        }

        public Vector2 LeftThumbstick()
        {
            return currentGamePadStates[0].ThumbSticks.Left;
        }

        public Vector2 RightThumbstick()
        {
            return currentGamePadStates[0].ThumbSticks.Right;
        }

        public double LeftThumbstickDirection()
        {
            return Math.Atan2(-currentGamePadStates[0].ThumbSticks.Left.Y, currentGamePadStates[0].ThumbSticks.Left.X) + MathHelper.PiOver2;
        }

        public double RightThumbstickDirection()
        {
            return Math.Atan2(-currentGamePadStates[0].ThumbSticks.Right.Y, currentGamePadStates[0].ThumbSticks.Right.X) + MathHelper.PiOver2;
        }

        #endregion

        #region Specific PlayerIndex Functions

        public bool IsButtonDown(PlayerIndex index, Buttons button)
        {
            if (currentGamePadStates[(int)index].IsButtonDown(button))
                return true;
            else
                return false;
        }

        public bool IsButtonPressed(PlayerIndex index, Buttons button)
        {
            if (currentGamePadStates[(int)index].IsButtonDown(button) && previousGamePadStates[(int)index].IsButtonUp(button))
                return true;
            else
                return false;
        }

        public bool IsButtonUp(PlayerIndex index, Buttons button)
        {
            if (currentGamePadStates[(int)index].IsButtonUp(button))
                return true;
            else
                return false;
        }

        public Vector2 LeftThumbstick(PlayerIndex index)
        {
            return currentGamePadStates[(int)index].ThumbSticks.Left;
        }

        public Vector2 RightThumbstick(PlayerIndex index)
        {
            return currentGamePadStates[(int)index].ThumbSticks.Right;
        }

        public double LeftThumbstickDirection(PlayerIndex index)
        {
            return Math.Atan2(-currentGamePadStates[(int)index].ThumbSticks.Left.Y, currentGamePadStates[(int)index].ThumbSticks.Left.X) + MathHelper.PiOver2;
        }

        public double RightThumbstickDirection(PlayerIndex index)
        {
            return Math.Atan2(-currentGamePadStates[(int)index].ThumbSticks.Right.Y, currentGamePadStates[(int)index].ThumbSticks.Right.X) + MathHelper.PiOver2;
        }

        public GamePadButtons GetPressedButtons(PlayerIndex index)
        {
            return currentGamePadStates[(int)index].Buttons;
        }

        #endregion

        #region Ranged Player Indexes Functions

        public bool IsButtonDown(PlayerIndex startingIndex, PlayerIndex endingIndex, Buttons button)
        {
            bool[] temp = new bool[4];
            for (int i = 0; i < temp.Length; i++)
            {temp[i] = false;}
            for (int i = (int)startingIndex; i < (int)endingIndex + 1; i++)
            {
                if (currentGamePadStates[i].IsButtonDown(button))
                    temp[i] = true;
                else
                    temp[i] = false;
            }
            for (int i = (int)startingIndex; i < (int)endingIndex; i++)
            {
                if (!temp[i])
                    return false;
            }
            return true;
        }

        public bool IsButtonPressed(PlayerIndex startingIndex, PlayerIndex endingIndex, Buttons button)
        {
            bool[] temp = new bool[4];
            for (int i = 0; i < temp.Length; i++)
            { temp[i] = false; }
            for (int i = (int)startingIndex; i < (int)endingIndex + 1; i++)
            {
                if (currentGamePadStates[i].IsButtonDown(button) && previousGamePadStates[i].IsButtonUp(button))
                    temp[i] = true;
                else
                    temp[i] = false;
            }
            for (int i = (int)startingIndex; i < (int)endingIndex; i++)
            {
                if (!temp[i])
                    return false;
            }
            return true;
        }

        public bool IsButtonUp(PlayerIndex startingIndex, PlayerIndex endingIndex, Buttons button)
        {
            bool[] temp = new bool[4];
            for (int i = 0; i < temp.Length; i++)
            { temp[i] = false; }
            for (int i = (int)startingIndex; i < (int)endingIndex + 1; i++)
            {
                if (currentGamePadStates[i].IsButtonUp(button))
                    temp[i] = true;
                else
                    temp[i] = false;
            }
            for (int i = (int)startingIndex; i < (int)endingIndex; i++)
            {
                if (!temp[i])
                    return false;
            }
            return true;
        }

        public List<Vector2> LeftThumbstick(PlayerIndex startingIndex, PlayerIndex endingIndex)
        {
            List<Vector2> thumbsticks = new List<Vector2>();
            for (int i = (int)startingIndex; i < (int)endingIndex + 1; i++)
            {
                thumbsticks.Add(currentGamePadStates[i].ThumbSticks.Left);
            }
            return thumbsticks;
        }

        public List<Vector2> RightThumbstick(PlayerIndex startingIndex, PlayerIndex endingIndex)
        {
            List<Vector2> thumbsticks = new List<Vector2>();
            for (int i = (int)startingIndex; i < (int)endingIndex + 1; i++)
            {
                thumbsticks.Add(currentGamePadStates[i].ThumbSticks.Right);
            }
            return thumbsticks;
        }

        public List<double> LeftThumbstickDirection(PlayerIndex startingIndex, PlayerIndex endingIndex)
        {
            List<double> directions = new List<double>();
            for (int i = (int)startingIndex; i < (int)endingIndex + 1; i++)
            {
                directions.Add(Math.Atan2(-currentGamePadStates[i].ThumbSticks.Left.Y, currentGamePadStates[i].ThumbSticks.Left.X));
            }
            return directions;
        }

        public List<double> RightThumbstickDirection(PlayerIndex startingIndex, PlayerIndex endingIndex)
        {
            List<double> directions = new List<double>();
            for (int i = (int)startingIndex; i < (int)endingIndex; i++)
            {
                directions.Add(Math.Atan2(-currentGamePadStates[i].ThumbSticks.Right.Y, currentGamePadStates[i].ThumbSticks.Right.X));
            }
            return directions;
        }

        #endregion

    }
}
